﻿// Fill out your copyright notice in the Description page of Project Settings.


#include "Character/EnemyCharacter.h"

#include "Actor/HealthPack.h"
#include "AIController/Enemy2AIController.h"
#include "AIController/EnemyAIController.h"
#include "Character/PlayerCharacter.h"
#include "Components/CapsuleComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GameMode/CombatGameMode.h"
#include "Helper/DebugHelper.h"
#include "Kismet/GameplayStatics.h"
#include "Kismet/KismetSystemLibrary.h"
#include "UMG/HealthBarWidget.h"


// Sets default values
AEnemyCharacter::AEnemyCharacter()
{
	// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	bUseControllerRotationPitch = false;
	bUseControllerRotationRoll = false;
	bUseControllerRotationYaw = false;

	UCharacterMovementComponent* CharacterMovementComponent = GetCharacterMovement();
	// 设置血条组件
	WidgetComponent = CreateDefaultSubobject<UWidgetComponent>("HealthBar");
	WidgetComponent->SetupAttachment(RootComponent);
	CharacterMovementComponent->bUseControllerDesiredRotation = true;
}

// Called when the game starts or when spawned
void AEnemyCharacter::BeginPlay()
{
	Super::BeginPlay();
	// Debug::printLog("enemy beginPlay");
	// // 默认开始enemy就是受伤害状态
	// IsDamage_AI = true;
	// AEnemyAIController* enemyAIController = Cast<AEnemyAIController>(GetController());
	// if (enemyAIController)
	// {
	// 	//Debug::printLog("enemy begin be damaged");
	// 	enemyAIController->SetAIDamage();
	// }

	//把血条widget交给组件
	UHealthBarWidget* HealthBarWidget = CreateWidget<UHealthBarWidget>(GetWorld(), HealthBarWidgetClass);
	WidgetComponent->SetWidget(HealthBarWidget);
	HealthBarWidget->Owner = this;
}

// Called every frame
void AEnemyCharacter::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

// Called to bind functionality to input
void AEnemyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
}

void AEnemyCharacter::PlayAttackMontage()
{
	//Debug::printLog("enemy attack");
	IsAttacking_AI = true;
	PlayMontage(AttackMontage);
}

void AEnemyCharacter::AttackDamage()
{
	// Super::AttackDamage();
	// Debug::printLog("player do attackDamage");
	FVector start = this->GetActorLocation();
	FVector end = start + this->GetActorForwardVector() * 200;
	TArray<FHitResult> OutHits;
	const TArray<AActor*> ActorsToIgnore;
	bool bSphereTraceMultiByProfile = UKismetSystemLibrary::SphereTraceMultiByProfile(
		GetWorld(), start, end, AttackRadius, "",
		true, ActorsToIgnore, EDrawDebugTrace::None, OutHits, true,
		FLinearColor::Blue, FLinearColor::Red, 5.0f);
	TArray<ABaseCharacter*> haveHit;
	if (bSphereTraceMultiByProfile)
	{
		for (FHitResult res : OutHits)
		{
			APlayerCharacter* player = Cast<APlayerCharacter>(res.GetActor());
			if (player && !haveHit.Contains(player))
			{
				player->TakeHitDamage(10.0f);
				haveHit.AddUnique(player);
			}
		}
	}
}

void AEnemyCharacter::TakeHitDamage(const float DamageValue)
{
	Super::TakeHitDamage(DamageValue);

	AEnemyAIController* enemyAIController = Cast<AEnemyAIController>(GetController());
	if (enemyAIController)
	{
		enemyAIController->SetAIDamage();
	}
}

void AEnemyCharacter::DeathClear()
{
	//血条widget组件销毁
	WidgetComponent->DestroyComponent();
	// 停止ai控制
	AEnemyAIController* EnemyAIController = Cast<AEnemyAIController>(GetController());
	if (EnemyAIController)
	{
		EnemyAIController->BrainComponent->StopLogic("");
		EnemyAIController->ClearFocus(EAIFocusPriority::Default);
	}
	// 停止运动
	GetCharacterMovement()->SetMovementMode(MOVE_None);
	GetCapsuleComponent()->SetCollisionEnabled(ECollisionEnabled::Type::NoCollision);

	// 销毁自身
	ACombatGameMode* GameMode = Cast<ACombatGameMode>(UGameplayStatics::GetGameMode(GetWorld()));
	GameMode->EnemyNum = GameMode->EnemyNum - 1;
	if (GameMode->EnemyNum <= 5)
	{
		GameMode->CanCreateBoss = true;
	}
	FLatentActionInfo info;
	info.Linkage = 1;
	info.UUID = 100;
	info.CallbackTarget = this;
	info.ExecutionFunction = "DestroySelf";
	UKismetSystemLibrary::Delay(GetWorld(), 3.0f, info);
	// 生成加血包
	if (HealthPackClass)
	{
		AHealthPack* HealthPack = GetWorld()->SpawnActor<AHealthPack>(HealthPackClass, GetTransform());
	}
}

void AEnemyCharacter::DestroySelf()
{
	//Debug::printLog("Destroy");
	// 销毁特效
	if (DeathParticle)
	{
		UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), DeathParticle, GetActorLocation());
	}
	Destroy();
}
